'''
Author: @zhyy
Date: 2021-02-01 15:39:04
LastEditors: @zhyy
LastEditTime: 2021-02-01 16:58:56
'''
import functools
from flask import g

def login_required(f):
    '让装饰器装饰的函数属性不会变 -- name属性'
    '第1种方法,使用functools模块的wraps装饰内部函数'
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        if not g.user_id:
            return {'message': 'User must be authorized.'}, 401
        elif g.is_refresh_token:
            return {'message': 'Do not use refresh token.'}, 403
        else:
            return f(*args, **kwargs)
  
    return wrapper

def super_required(f):
    @functools.wraps(f)  # 保留源信息，本质是endpoint装饰，否则修改函数名很危险
    def inner(*args, **kwargs): 
        if not g.user_id:
            return {'message': 'User must be authorized.'}, 401
        if g.is_refresh_token:
            return {'message': 'Do not use refresh token.'}, 403
        scope=g.scope
        if scope=="super":
            return f(*args, **kwargs)
        else:
            return {'message': 'User have  no permission'}, 401     
    return inner

